package com.foreknow.controller;

import com.foreknow.entity.Reply;
import com.foreknow.service.ReplyService;
import com.foreknow.service.UserService;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet({ "/api/reply", "/api/blogReplies", "/api/replyReply" })
public class ReplyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ReplyService replyService;
	private UserService userService;
	private Gson gson;
	private Map<String, Object> data;

	public ReplyServlet() {
		super();
		replyService = new ReplyService();
		userService = new UserService();
		gson = new Gson();
		data = new HashMap<>();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uri = request.getRequestURI();
		if (uri.endsWith("/api/blogReplies")) {
			Integer blogID = Integer.valueOf(request.getParameter("blogId"));
			List<Reply> replies = replyService.getRepliesByBlogId(blogID);
			if (replies == null) {
				data.put("errorCode", -1);
				data.put("message", "获取博客回复失败");
				System.out.println(blogID);
			} else {
				for (Reply reply : replies) {
//					回复人姓名
					reply.setAuthorName(userService.getUserByID(reply.getAuthorId()).getUsername());
					if (reply.getAnswererId() != 0 && reply.getAnswererId() != null) {
//						回复回复的人的姓名
						reply.setAnswererName(userService.getUserByID(reply.getAnswererId()).getUsername());
					}
				}
				data.put("errorCode", 0);
				data.put("message", "获取博客回复成功");
				data.put("replies", replies);
			}
		} else if (uri.endsWith("/api/reply")) {// 单个blog
			Integer id = Integer.valueOf(request.getParameter("id"));
			Reply reply = replyService.getReplyById(id);
			if (reply != null) {
				reply.setAuthorName(userService.getUserByID(reply.getAuthorId()).getUsername());
				if (reply.getAnswererId() != null) {
					reply.setAnswererName(userService.getUserByID(reply.getAnswererId()).getUsername());
				}
				data.put("errorCode", 0);
				data.put("message", "获取博客回复成功");
				data.put("reply", reply);
			} else {
				data.put("errorCode", -1);
				data.put("message", "获取博客回复失败");
			}
		}
		String json = gson.toJson(data);
		response.getWriter().write(json);
		data.clear();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uri = request.getRequestURI();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Reply reply = null;
		if (uri.endsWith("/api/reply")) {
			Integer authorId = Integer.valueOf(request.getParameter("authorId"));
			Integer blogId = Integer.valueOf(request.getParameter("blogId"));
			String content = request.getParameter("content");
			String uploadTime = dateFormat.format(new Date());
			reply = new Reply(blogId, null, authorId, null, content, uploadTime);

		} else if (uri.endsWith("/api/replyReply")) {
			Integer authorId = Integer.valueOf(request.getParameter("authorId"));
			Integer replyId = Integer.valueOf(request.getParameter("replyId"));
			Integer answererId = Integer.valueOf(request.getParameter("answererId"));
			Integer blogId = Integer.valueOf(request.getParameter("blogId"));
			String content = request.getParameter("content");
			String uploadTime = dateFormat.format(new Date());
			reply = new Reply(blogId, replyId, answererId, authorId, content, uploadTime);
		}
		if (replyService.insertReply(reply) > 0) {
			System.out.println("OK");
			data.put("errorCode", 0);
			data.put("message", "回复成功");
		} else {
			System.out.println("error");
			data.put("errorCode", -1);
			data.put("message", "回复失败");
		}
		String json = gson.toJson(data);
		response.getWriter().write(json);
		data.clear();
	}
}
